package com.yizhe.interceptor;

import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Jiang,JiaYong
 * @version 2014-03-21
 */
public class ReturnViewInterceptor implements HandlerInterceptor {
	protected final Logger logger = LoggerFactory.getLogger(super.getClass());

	/**
	 * 以什么字符串开头，排除地址【多个以英文逗号分隔】
	 */
	private String exclude_startwith;

	/**
	 * 包含什么字符串排除【多个以英文逗号分隔】
	 */
	private String exclude_indexof;

	/**
	 * 1，拦截处理，返回true继续往下走，顺向的连接器链
	 */
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		return true;
	}

	/**
	 * 2，控制器已经执行完，在生成视图执行可以做一些动作，逆向的拦截器链
	 */
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView modelAndView)
			throws Exception {
		// 等于Null的条件主要是我们使用 render返回，比如返回javascript，text，json等
		if (null == modelAndView) {
			return;
		}
		// 获取返回逻辑View视图名称
		String viewName = modelAndView.getViewName();

		// 判断以某个字符串开头不处理
		if (StringUtils.isNotBlank(exclude_startwith)) {
			StringTokenizer token = new StringTokenizer(exclude_startwith, ",");
			while (token.hasMoreElements())
				if (StringUtils.startsWith(viewName, token.nextToken()))
					return;
		}

		// 判断以包含某个字符串不处理
		if (StringUtils.isNotBlank(exclude_indexof)) {
			StringTokenizer token = new StringTokenizer(exclude_indexof, ",");
			while (token.hasMoreElements())
				if (-1 != StringUtils.indexOf(viewName, token.nextToken()))
					return;
		}

		// 得到请求的地址
		String servletPath = request.getServletPath();

		// 获得去除最后的xxx.do地址以后
		String dealPathBefore = StringUtils.substringBeforeLast(servletPath, "/");

		// 这个判断是处理 htttp://localhsot:8080/manager/SysSupplier.do 这样的请求
		if (StringUtils.countMatches(servletPath, "/") <= 2) {
			dealPathBefore = StringUtils.substringBeforeLast(servletPath, ".");
		}

		// 为空则请求的地址是在项目地下无目录，比如:htttp://localhsot:8080/hospital/index.do
		if (StringUtils.isBlank(dealPathBefore)) {
			return;
		}

		// 去除开头的斜杠 "/"
		String dealPathLast = StringUtils.substringAfter(dealPathBefore, "/");

		// 返回处理以后的视图名称
		String returnViewName = dealPathLast + "/" + viewName;
		System.out.println("返回处理以后的视图名称:"+returnViewName);
		// 返回
		//modelAndView.setViewName(returnViewName);
	}

	/**
	 * 3，视图已经生成展示完，这个方法一般执行释放资源的作用
	 */
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception exception)
			throws Exception {
	}

	public String getCtxPath(HttpServletRequest request) {
		StringBuffer ctx = new StringBuffer();
		ctx.append(request.getScheme()).append("://").append(request.getServerName()).append(":").append(request.getServerPort());
		ctx.append(request.getContextPath());
		return ctx.toString();
	}

	public String getExclude_startwith() {
		return exclude_startwith;
	}

	public void setExclude_startwith(String exclude_startwith) {
		this.exclude_startwith = exclude_startwith;
	}

	public String getExclude_indexof() {
		return exclude_indexof;
	}

	public void setExclude_indexof(String exclude_indexof) {
		this.exclude_indexof = exclude_indexof;
	}

}
